libname phy 'R:\Active\physician\Data'; run; /* libname to my physician compare data */
libname temp 'R:\Active\mcbs\SAS\data\temp'; /* save temporary datasets here */

libname xptfile xport 'R:\Active\physician\Data\phy_comp_201512.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

Data phy_comp_2015 (keep = PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro);
set phy.phy_comp;
PRF_NPI = put(npi,10.);

if PRIMARYS='GERIATRIC MEDICINE' or SECONDAR='GERIATRIC MEDICINE' or SECONDA2='GERIATRIC MEDICINE' or SECONDA3='GERIATRIC MEDICINE' or SECONDA4='GERIATRIC MEDICINE' 
or (find(ALLSECON,'GERIATRIC MEDICINE','i') ge 1) then phycomp_ger_med=1; 

if PRIMARYS in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDAR in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA2 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA3 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA4 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or
(find(ALLSECON,'PSYCHIATRY','i') ge 1)
then phycomp_psychiatrist=1; 

if PRIMARYS in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDAR in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA2 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA3 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA4 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or
(find(ALLSECON,'NEURO','i') ge 1)
then phycomp_neuro=1; 

if phycomp_ger_med=1 or phycomp_psychiatrist=1 or phycomp_neuro=1;

run; 

libname xptfile xport 'R:\Active\physician\Data\phy_comp_201612.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

Data phy_comp_2016 (keep = PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro);
set phy.phy_comp;
PRF_NPI = put(npi,10.);

if PRIMARYS='GERIATRIC MEDICINE' or SECONDAR='GERIATRIC MEDICINE' or SECONDA2='GERIATRIC MEDICINE' or SECONDA3='GERIATRIC MEDICINE' or SECONDA4='GERIATRIC MEDICINE' 
or (find(ALLSECON,'GERIATRIC MEDICINE','i') ge 1) then phycomp_ger_med=1; 

if PRIMARYS in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDAR in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA2 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA3 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SECONDA4 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or
(find(ALLSECON,'PSYCHIATRY','i') ge 1)
then phycomp_psychiatrist=1; 

if PRIMARYS in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDAR in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA2 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA3 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SECONDA4 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or
(find(ALLSECON,'NEURO','i') ge 1)
then phycomp_neuro=1; 

if phycomp_ger_med=1 or phycomp_psychiatrist=1 or phycomp_neuro=1;

run; 

libname xptfile xport 'R:\Active\physician\Data\phy_comp_201712.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

Data phy_comp_2017 (keep = PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro);
set phy.phy_comp;
PRF_NPI = put(npi,10.);

if PRI_SPEC='GERIATRIC MEDICINE' or SEC_SPEC='GERIATRIC MEDICINE' or SEC_SPE2='GERIATRIC MEDICINE' or SEC_SPE3='GERIATRIC MEDICINE' or SEC_SPE4='GERIATRIC MEDICINE' 
or (find(SEC_SPE5,'GERIATRIC MEDICINE','i') ge 1) then phycomp_ger_med=1; 

if PRI_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE2 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE3 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE4 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or
(find(SEC_SPE5,'PSYCHIATRY','i') ge 1)
then phycomp_psychiatrist=1; 

if PRI_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE2 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE3 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE4 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or
(find(SEC_SPE5,'NEURO','i') ge 1)
then phycomp_neuro=1; 

if phycomp_ger_med=1 or phycomp_psychiatrist=1 or phycomp_neuro=1;

run; 

libname xptfile xport 'R:\Active\physician\Data\phy_comp_201812.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

Data phy_comp_2018 (keep = PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro);
set phy.phy_comp;
PRF_NPI = put(npi,10.);

if PRI_SPEC='GERIATRIC MEDICINE' or SEC_SPEC='GERIATRIC MEDICINE' or SEC_SPE2='GERIATRIC MEDICINE' or SEC_SPE3='GERIATRIC MEDICINE' or SEC_SPE4='GERIATRIC MEDICINE' 
or (find(SEC_SPE5,'GERIATRIC MEDICINE','i') ge 1) then phycomp_ger_med=1; 

if PRI_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE2 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE3 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE4 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or
(find(SEC_SPE5,'PSYCHIATRY','i') ge 1)
then phycomp_psychiatrist=1; 

if PRI_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE2 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE3 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE4 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or
(find(SEC_SPE5,'NEURO','i') ge 1)
then phycomp_neuro=1; 

if phycomp_ger_med=1 or phycomp_psychiatrist=1 or phycomp_neuro=1;

run;

libname xptfile xport 'R:\Active\physician\Data\phy_comp_201912.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

Data phy_comp_2019 (keep = PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro);
set phy.phy_comp;
PRF_NPI = put(npi,10.);

if PRI_SPEC='GERIATRIC MEDICINE' or SEC_SPEC='GERIATRIC MEDICINE' or SEC_SPE2='GERIATRIC MEDICINE' or SEC_SPE3='GERIATRIC MEDICINE' or SEC_SPE4='GERIATRIC MEDICINE' 
or (find(SEC_SPE5,'GERIATRIC MEDICINE','i') ge 1) then phycomp_ger_med=1; 

if PRI_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPEC in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE2 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE3 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or 
SEC_SPE4 in ('GERIATRIC PSYCHIATRY','NEUROPSYCHIATRY','PSYCHIATRY') or
(find(SEC_SPE5,'PSYCHIATRY','i') ge 1)
then phycomp_psychiatrist=1; 

if PRI_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPEC in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE2 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE3 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or 
SEC_SPE4 in ('NEUROLOGY','NEUROPSYCHIATRY','NEUROSURGERY') or
(find(SEC_SPE5,'NEURO','i') ge 1)
then phycomp_neuro=1; 

if phycomp_ger_med=1 or phycomp_psychiatrist=1 or phycomp_neuro=1;

run;  

data phycomp;
set phy_comp_2015 phy_comp_2016 phy_comp_2017 phy_comp_2018 phy_comp_2019;
run;

proc sort data = phycomp; by PRF_NPI; run; 
proc summary data = phycomp;
by PRF_NPI;
var phycomp_ger_med phycomp_psychiatrist phycomp_neuro;
output out=phycomp(keep=PRF_NPI phycomp_ger_med phycomp_psychiatrist phycomp_neuro)sum=;
run;

data phycomp;
set phycomp;
if phycomp_ger_med>=1 then phycomp_ger_med=1; else phycomp_ger_med=0;
if phycomp_psychiatrist>=1 then phycomp_psychiatrist=1; else phycomp_psychiatrist=0;
if phycomp_neuro>=1 then phycomp_neuro=1; else phycomp_neuro=0;
run;

/*libname xptfile xport 'S:\Johnston\physician\Data\phy_nppes_2022.xpt' access=readonly;
proc copy inlib=xptfile outlib=phy;
run; 

data phy.nppes_npi_phy (keep = prf_npi prv_taxonomy1--prv_taxonomy15);
set phy.phy_nppe;
prv_taxonomy1=prvtaxon;
prv_taxonomy2=PRVTAXO2;
prv_taxonomy3=PRVTAXO3;
prv_taxonomy4=PRVTAXO4;
prv_taxonomy5=PRVTAXO5;
prv_taxonomy6=PRVTAXO6;
prv_taxonomy7=PRVTAXO7;
prv_taxonomy8=PRVTAXO8;
prv_taxonomy9=PRVTAXO9;
prv_taxonomy10=PRVTAX10;
prv_taxonomy11=PRVTAX11;
prv_taxonomy12=PRVTAX12;
prv_taxonomy13=PRVTAX13;
prv_taxonomy14=PRVTAX14;
prv_taxonomy15=PRVTAX15;
run;*/

data phy_nppes (keep = PRF_NPI phynppes_ger_med phynppes_psych_med phynppes_neuro phynppes_mhbh phynppes_ger_spc apn_pcp);
set phy.nppes_npi_phy;

phynppes_ger_med=0;
phynppes_psych_med=0;
phynppes_neuro=0;
phynppes_mhbh=0;
phynppes_ger_spc=0;
apn_pcp=0;
array prv{15} prv_taxonomy1-prv_taxonomy15;
do i=1 to 15;

/* geriatrics */
if prv{i} in ('207QG0300X','207RG0300X')
then phynppes_ger_med=1; 

/* psychiatry */
if prv{i} in ('2084H0002X','2084P0015X','2084S0012X','2084P0800X','2084P0802X','2084B0040X','2084P0805X','364SP0808X','364SP0809X','364SP0811X','364SP0813X','363LP0808X') 
then phynppes_psych_med=1; 

/* neurology */
if prv{i} in ('2081P0301X','2081N0008X','2084P2900X','2084B0040X','2084P0301X','2084N0600X','2084A2900X','2084P0005X','2084N0008X','2084N0400X','2084V0102X','2085N0700X',
					 '2085D0003X') 
then phynppes_neuro=1;

/* psychology, mental health, behavioral health, counseling */
if prv{i} in ('106E00000X','103K00000X','103G00000X','101Y00000X','101YM0800X','101YP2500X','103T00000X','103TA0700X','103TC0700X','103TB0200X','103TC1900X','103TF0000X',
					 '103TP2701X','103TH0004X','103TH0100X','103TM1800X','103TP0016X','103TR0400X','103TW0100X','104100000X','1041C0700X')
then phynppes_mhbh=1;

/* Advanced practice non-physician PCP */
if prv{i} in ('363L00000X','363LA2200X','363LC1500X','363LF0000X','363LP2300X','363LW0102X','363A00000X','363AM0700X')
then apn_pcp=1; 
end;

if phynppes_ger_med=1 or phynppes_psych_med=1 or phynppes_neuro=1 or phynppes_mhbh=1 then phynppes_ger_spc=1; 
if phynppes_ger_spc=1 or apn_pcp=1; 
run;

proc sort data = phy_nppes nodupkey; by prf_npi; run; /* there are no dupes */

proc means data = phy_nppes n sum mean;
var phynppes_mhbh phynppes_ger_med phynppes_psych_med phynppes_neuro apn_pcp;
run;

data temp.phy_ger_psychia_neuro (keep = PRF_NPI phy_geriatrics phy_psychiatrist phy_neuro geriatric_spc pmbh_coun apn_pcp);
/*merge phycomp phy_nppes;*/
set phy_nppes;
by PRF_NPI;
if /*phycomp_ger_med=1 or */ phynppes_ger_med=1 then phy_geriatrics=1; else phy_geriatrics=0;
if /* phycomp_psychiatrist=1 or */ phynppes_psych_med=1 then phy_psychiatrist=1; else phy_psychiatrist=0;
if /*phycomp_neuro=1 or */ phynppes_neuro=1 then phy_neuro=1; else phy_neuro=0;
if phynppes_ger_spc=1 then geriatric_spc=1; else geriatric_spc=0;
if phynppes_mhbh=1 then pmbh_coun=1; else pmbh_coun=0;
if apn_pcp=. then apn_pcp=0;
run;

proc sort data = temp.phy_ger_psychia_neuro nodupkey;
by PRF_NPI;
run; 

/*******************************		END CODE		**********************/
